package com.jelly.web.modules.system.permission.controller;

import com.jelly.common.constant.Constant;
import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.note.OperationLog;
import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.base.domain.Base;
import com.jelly.web.modules.system.permission.domain.Permission;
import com.jelly.web.modules.system.permission.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @创建人 ql
 * @创建时间 2018-09-08 16:00
 * @项目名称 jelly
 * @功能描述: 菜单权限控制器
 **/
@Controller
@RequestMapping("/admin/system/permission")
public class PermissionController extends BaseController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 菜单权限列表跳转
     * @return
     */
    @GetMapping
    public String getPermissionList(){

        //获取字典详情
        getDictionary();

        //获取该用户可访问的按钮详情
        getButton();

        return adminSystemPermission("/list");

    }

    /**
     * 菜单权限列表详情
     * @param permission
     * @return
     */
    @PostMapping
    @ResponseBody
    public List<Permission> getPermissionList(Permission permission){

        return permissionService.getPermissionList(permission);

    }

    /**
     * 修改菜单权限跳转
     * @param model
     * @param base
     * @return
     */
    @GetMapping("/update")
    public String editPermission(Model model, Base base){
        //获取字典详情
        getDictionary();
        List<Permission> list = permissionService.getPermissionList(Constant.SUCCESS);
        Permission permission = new Permission();
        permission.setPerId("0");
        permission.setPerName("顶级菜单");
        list.add(permission);
        model.addAttribute("permissionList",list);
        if(StringUtils.isNotBlank(base.getBaseId())){
            model.addAttribute("permission",permissionService.getPermissionId(base.getBaseId()));
        }
        return adminSystemPermission("/update");
    }

    /**
     * 修改菜单权限
     * @param permission
     * @return
     */
    @PostMapping("/update")
    @OperationLog(name = "菜单模块",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> editPermission(Permission permission){

        if(StringUtils.isNotBlank(permission.getPerIcon())){
            permission.setPerIcon("layui-icon "+permission.getPerIcon());
        }

        if(permissionService.updatePermission(permission)){
            return okResponse("修改成功");
        }

        return errorResponse("修改失败");

    }

    /**
     * 新增菜单菜单跳转
     * @param model
     * @return
     */
    @GetMapping("/add")
    public String addPermission(Model model){
        //获取字典详情
        getDictionary();

        model.addAttribute("permissionList",permissionService.getPermissionList(Constant.SUCCESS));

        return adminSystemPermission("/add");
    }
    /**
     * 新增菜单权限
     * @param permission
     * @return
     */
    @PostMapping("/add")
    @OperationLog(name = "菜单模块",value = GlobalConstant.ADDED)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> addPermission(Permission permission){

        if(permissionService.addPermission(permission)){
            return okResponse("新增成功");
        }
        return errorResponse("新增失败");

    }

    /**
     * 删除菜单权限
     * @param base
     * @return
     */
    @PostMapping("/delect")
    @OperationLog(name = "菜单模块",value = GlobalConstant.DELECT)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectPermission(Base base){

        if(StringUtils.isNotBlank(base.getBaseId())){
            if(permissionService.delectPermission(base.getBaseId())){
                return okResponse("删除成功");
            }
        }

        return errorResponse("删除失败");

    }
    /**
     * @param model
     * @return
     */
    @GetMapping("/level")
    public String treePermission(Model model,Permission permission){
        model.addAttribute("treePermission",permissionService.getTreePermission(permission));
        return adminSystemPermission("/level");
    }


    /**
     * 权限信息验证
     * @param permission
     * @return
     */
    @PostMapping("/verify")
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> userVerify(Permission permission){

        if(StringUtils.isNotBlank(permission.getPerName()) || StringUtils.isNotBlank(permission.getPerRoute()) || StringUtils.isNotBlank(permission.getPerHref())){
            List<Permission> list = permissionService.getPermissionList(permission);
            if(ListUtil.isNotList(list)){
                if(StringUtils.isNotBlank(permission.getPerName())){
                    return errorResponse("菜单名称重复");
                }else if (StringUtils.isNotBlank(permission.getPerRoute())){
                    return errorResponse("权限标识重复");
                }else {
                    return errorResponse("链接重复");
                }
            }
        }
        return okResponse("");

    }
}
